﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

public partial class _VideoArchive : System.Web.UI.Page
{
    protected LocalFunctions FUNC;
    private int iTotalPages = 0;
    private int iPageSize = 2;
    public int CurrentPage = 1;

    protected string sKeywords, sDescription;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Rondo)Master).FUNC;
        sendto.FUNC = FUNC;

        lnkGallery.Attributes.Add("style", (FUNC.LangID == 1) ? FUNC.sSelected : FUNC.sSelectedHeb);
        GetParameters();
        Title = FUNC.GetPhrase(13);

        Dal.arParams.Clear();
        Dal.arParams.Add(8); // vidio galery
        Dal.arParams.Add(FUNC.LangID);
        DataTable DTtitle = Dal.ExecuteDBsp("RD_SelectStaticPageById", Dal.arParams);
        if (DTtitle != null)
        {
            if (DTtitle.Rows.Count > 0)
            {
                Title = DTtitle.Rows[0]["BrowserTitle"].ToString();
                sKeywords = DTtitle.Rows[0]["Keywords"].ToString();
                sDescription = DTtitle.Rows[0]["Description"].ToString();
                imgTitle.Src = FUNC.FullAppPath + "/Gallery/" + DTtitle.Rows[0]["imageLink"].ToString();
            }
        }

        Dal.arParams.Clear();
        Dal.arParams.Add(FUNC.LangID);
        Dal.arParams.Add(CurrentPage);
        Dal.arParams.Add(iPageSize);
        Dal.arParams.Add(1); // only video galeries
        DataTable DT = Dal.ExecuteDBsp("RD_SelectAllGalleries", Dal.arParams);
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                iTotalPages = (int)Math.Ceiling((double)Convert.ToInt32(DT.Rows[0]["TotalRows"]) / iPageSize);
                dlGallery.DataSource = DT;
                dlGallery.DataBind();
            }
        SimplePager();
    }

    private void GetParameters()
    {
        CurrentPage = Convert.ToInt32(Request.QueryString["page"]);
    }

    protected void dlGallery_DataBind(object sender, DataListItemEventArgs e)
    {
        int iGaleryID = Convert.ToInt32(((HiddenField)e.Item.FindControl("hfGalID")).Value);
        DataList dlImages = ((DataList)e.Item.FindControl("rptGalTopImages"));

        Dal.arParams.Clear();
        Dal.arParams.Add(iGaleryID);

        DataTable DTImages = Dal.ExecuteDBsp("RD_SelectTopVideoImages", Dal.arParams);
        if (DTImages != null)
            if (DTImages.Rows.Count > 0)
            {
                dlImages.DataSource = DTImages;
                dlImages.DataBind();
            }
    }

    protected void rptGalTopImages_DataBind(object sender, DataListItemEventArgs e)
    {
        HtmlImage htmImg = (HtmlImage)e.Item.FindControl("imgGallery");
        string sLink = htmImg.Src;
        
        Regex regex = new Regex(@"v\=([\-\w]+)");

        Match m = regex.Match(sLink);
        string mValue = m.Value.Replace("v=", "");
        string sImageLink = string.Format("../Thumbnails/Thumbnail.ashx?img=http://img.youtube.com/vi/{0}/0.jpg&sizeW=120&sizeH=90", mValue);   
        htmImg.Src = sImageLink;
    }

    public void SimplePager()
    {
        if (CurrentPage < 0) CurrentPage = 1;
        if (iTotalPages < 0) iTotalPages = 0;

        var pager = new PagerBuilder();

        if (CurrentPage > 1)
        {
            pager.AddPage("&lt;&lt;", 1);
            pager.AddPage("&lt;", CurrentPage - 1);
        }

        var start = Math.Max(CurrentPage - 2, 1);
        var end = Math.Min(iTotalPages, CurrentPage + 2);

        for (var i = start; i <= end; i++)
        {
            if (i == CurrentPage)
                pager.AddPage(i.ToString(), i, "font-weight: bold; color: White;");
            else
                pager.AddPage(i.ToString(), i);
        }

        if (CurrentPage < iTotalPages)
        {
            pager.AddPage("&gt;", CurrentPage + 1);
            pager.AddPage("&gt;&gt;", iTotalPages);
        }

        divPager.InnerHtml += pager.ToString();
    }

    public class PagerBuilder
    {
        private class PagerLink
        {
            public string Title { get; set; }
            public int PageNo { get; set; }
            public string Class { get; set; }
        }

        private readonly List<PagerLink> _pagerLinks =
                new List<PagerLink>();

        public PagerBuilder()
        {
        }

        public string PagerClass { get; set; }

        public void AddPage(string title, int pageNo)
        {
            AddPage(title, pageNo, string.Empty);
        }

        public void AddPage(string title, int pageNo,
                            string itemClass)
        {
            var link = new PagerLink
            {
                PageNo = pageNo,
                Title = title,
                Class = itemClass
            };
            _pagerLinks.Add(link);
        }

        public override string ToString()
        {
            var builder = new StringBuilder();

            foreach (var link in _pagerLinks)
            {
                builder.Append("<a");
                if (link.Class != string.Empty)
                {
                    builder.AppendFormat(" style=\"{0}\"", link.Class);
                }
                builder.Append(" href=\"");
                builder.AppendFormat("VideoArchive.aspx?page={0}", link.PageNo);
                builder.Append("\">");
                builder.Append(link.Title);
                builder.Append("</a>&nbsp;");
            }
            return builder.ToString();
        }
    }
}
